package com.magist.manage.copyrights.controller;

import com.github.pagehelper.PageInfo;
import com.magist.core.controller.BaseController;
import com.magist.core.entity.ResultMessage;
import com.magist.core.exception.BusinessException;
import com.magist.manage.copyrights.dto.SupplyLinkMansDto;
import com.magist.manage.copyrights.entity.SupplyLinkMans;
import com.magist.manage.copyrights.service.SupplyLinkMansService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@ApiIgnore
@Api(value = "RestSupplyLinkMansController", description = "供应商联系人接口")
@RestController
@RequestMapping("/rest/supplyLinkMans")

public class RestSupplyLinkMansController extends BaseController {



	@Autowired
	private SupplyLinkMansService supplyLinkMansService;

	@ApiOperation(value="保存", notes="保存接口")
	@RequestMapping(value = "save.do", method = RequestMethod.POST)
	public Object save(@ModelAttribute("supplyLinkMansDto") SupplyLinkMansDto supplyLinkMansDto) {
		SupplyLinkMans supplyLinkMans = new SupplyLinkMans();
		BeanUtils.copyProperties(supplyLinkMansDto,supplyLinkMans);
		supplyLinkMans.setId(supplyLinkMansService.getNewId());
		supplyLinkMansService.insert(supplyLinkMans);
		return ResultMessage.success();
	}

	@ApiOperation(value="更新", notes="更新接口")
	@RequestMapping(value = "update.do", method = RequestMethod.POST)
	public Object update(@ModelAttribute("supplyLinkMansDto") SupplyLinkMansDto supplyLinkMansDto) {
		SupplyLinkMans oldSupplyLinkMans = supplyLinkMansService.getById(supplyLinkMansDto.getId());
		if(oldSupplyLinkMans==null)
			throw new BusinessException("无效的数据!");
		ResultMessage result =null;
		SupplyLinkMans supplyLinkMans = new SupplyLinkMans();
		BeanUtils.copyProperties(supplyLinkMansDto,supplyLinkMans);
		supplyLinkMans.setModifyTime(oldSupplyLinkMans.getModifyTime());
		if (supplyLinkMansService.update(supplyLinkMans)>0) 
			result = ResultMessage.success("操作成功");
		else
			result=ResultMessage.error("操作失败!");
		return result;
	}

	@ApiOperation(value="列表", notes="分页查询接口")
	@RequestMapping(value = "page.do", method = RequestMethod.GET)
	public Object page(@ModelAttribute("supplyLinkMansDto") SupplyLinkMansDto supplyLinkMansDto) {
		ResultMessage result = ResultMessage.success();
		Example example = new Example(SupplyLinkMans.class);
		example = packExample(example,supplyLinkMansDto);
		PageInfo<SupplyLinkMans> pageInfo = supplyLinkMansService.page(example,getPageInfo());
		result.setContent(pageInfo);
		return result;
	}

	@ApiOperation(value="删除", notes="删除接口")
	@RequestMapping(value = "del.do", method = RequestMethod.POST)
	public Object del(@ModelAttribute("id") String id) {
		SupplyLinkMans supplyLinkMans = supplyLinkMansService.getById(id);
		if(supplyLinkMans==null)
			throw new BusinessException("无效的数据!");
		ResultMessage result =null;
		if(supplyLinkMansService.delete(supplyLinkMans)>0)
			result = ResultMessage.success("操作成功");
		else
			result=ResultMessage.error("操作失败!");
		return result;
	}

	@ApiOperation(value="详情", notes="详情接口")
	@RequestMapping(value = "info.do", method = RequestMethod.GET)
	public Object info(@ModelAttribute("id") String id) {
		SupplyLinkMans supplyLinkMans = supplyLinkMansService.getById(id);
		if(supplyLinkMans==null)
			throw new BusinessException("无效的数据!");
		SupplyLinkMansDto supplyLinkMansDto = new SupplyLinkMansDto();
		BeanUtils.copyProperties(supplyLinkMans,supplyLinkMansDto);
		ResultMessage result = ResultMessage.success();
		result.setContent(supplyLinkMansDto);
		return result;
	}



	@ApiOperation(value="供应商联系人列表", notes="供应商联系人列表")
	@RequestMapping(value = "supplyLinkManList.do", method = RequestMethod.GET)
	public Object supplyLinkManList(@ModelAttribute("SupplyLinkMansDto") SupplyLinkMansDto supplyLinkMansDto) {
		ResultMessage result = ResultMessage.success();
		Example example = new Example(SupplyLinkMans.class);
		example = packExample(example,supplyLinkMansDto);
		List<SupplyLinkMans> supplyLinkMans = supplyLinkMansService.find(example);
		result.setContent(supplyLinkMans);
		return result;
	}


	private Example packExample(Example example,SupplyLinkMansDto supplyLinkMansDto){
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("deleted","F");
		if(StringUtils.isNotEmpty(supplyLinkMansDto.getId()))
			criteria.andEqualTo("id",supplyLinkMansDto.getId());
		if(StringUtils.isNotEmpty(supplyLinkMansDto.getSupplyId()))
			criteria.andEqualTo("supplyId",supplyLinkMansDto.getSupplyId());
		return example;
	}
}
